﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.IO.IsolatedStorage;
using System.ComponentModel;
using System.Collections.Generic;
namespace Phone7ActionPack.Controls
{
    public class PhoneFileDataSource : FrameworkElement, INotifyPropertyChanged
    {

        IsolatedStorageFile appFiles;
        private System.Collections.ObjectModel.ObservableCollection<FileSystemItem> items = new System.Collections.ObjectModel.ObservableCollection<FileSystemItem>();

        public System.Collections.ObjectModel.ObservableCollection<FileSystemItem> FileSystemItems
        {
            get { return items; }
            set { items = value; }
        }
        public PhoneFileDataSource()
        {

            try
            {
                appFiles = IsolatedStorageFile.GetUserStoreForApplication();
                populateItems();
            }
            catch (IsolatedStorageException ex)
            {
                items.Add(new FileSystemItem()
                {
                    Name = "Documents",
                    FileSystemType = FileSystemItemTypes.Directory
                });
                items.Add(new FileSystemItem()
                {
                    Name = "System",
                    FileSystemType = FileSystemItemTypes.Directory
                });
                items.Add(new FileSystemItem()
                {
                    Name = "ReadMe.txt",
                    FileSystemType = FileSystemItemTypes.File
                });
                items.Add(new FileSystemItem()
                {
                    Name = "hello.world",
                    FileSystemType = FileSystemItemTypes.File
                });
                items.Add(new FileSystemItem()
                {
                    Name = "Phone7ActionPack.awesome",
                    FileSystemType = FileSystemItemTypes.File
                });
                items.Add(new FileSystemItem()
                {
                    Name = "Welcome.html",
                    FileSystemType = FileSystemItemTypes.File
                });

            }
        }



        private void populateItems()
        {
            if (appFiles == null)
                return;
            items.Clear();

            if (this.ShowDirectories)
                appFiles.GetDirectoryNames().ToList().ForEach(i => items.Add(new FileSystemItem() { FileSystemType = FileSystemItemTypes.Directory, FullPath = i, Name = i }));
            //appFiles.GetDirectoryNames(System.IO.Path.Combine(this.DirectoryPath, this.SearchPattern)).ToList().ForEach(i => items.Add(new FileSystemItem() { FileSystemType = FileSystemItemTypes.Directory, FullPath = i, Name = i }));
            if (this.ShowFiles)
                appFiles.GetFileNames().ToList().ForEach(i => items.Add(new FileSystemItem() { FileSystemType = FileSystemItemTypes.File, FullPath = i, Name = i }));
            this.items.OrderBy(i => i.Name);

            //   this.OnPropertyChanged("FileSystemItems");

        }

        public bool ShowFiles
        {
            get { return (bool)GetValue(ShowFilesProperty); }
            set { SetValue(ShowFilesProperty, value); }
        }

        public static readonly DependencyProperty ShowFilesProperty =
            DependencyProperty.Register("ShowFiles", typeof(bool), typeof(PhoneFileDataSource), new PropertyMetadata(true,ShowFilesProperty_Changed));

        private static void ShowFilesProperty_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var source = obj as PhoneFileDataSource;
            source.populateItems();
        }

        public bool ShowDirectories
        {
            get { return (bool)GetValue(ShowDirectoriesProperty); }
            set {SetValue(ShowDirectoriesProperty, value);}
        }

        public static readonly DependencyProperty ShowDirectoriesProperty =
            DependencyProperty.Register("ShowDirectories", typeof(bool), typeof(PhoneFileDataSource), new PropertyMetadata(false,ShowDirectoriesProperty_Changed));

        private static void ShowDirectoriesProperty_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var source = obj as PhoneFileDataSource;
            source.populateItems();
        }

        public string SearchPattern
        {
            get { return (string)GetValue(SearchPatternProperty); }
            set { SetValue(SearchPatternProperty, value); }
        }

        public static readonly DependencyProperty SearchPatternProperty =
            DependencyProperty.Register("SearchPattern", typeof(string), typeof(PhoneFileDataSource), new PropertyMetadata("", SearchPatternProperty_Changed));




        public string DirectoryPath
        {
            get { return (string)GetValue(DirectoryPathProperty); }
            set { SetValue(DirectoryPathProperty, value); }
        }

        public static readonly DependencyProperty DirectoryPathProperty =
            DependencyProperty.Register("DirectoryPath", typeof(string), typeof(PhoneFileDataSource), new PropertyMetadata("", DirectoryPathProperty_Changed));


        private static void SearchPatternProperty_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var source = obj as PhoneFileDataSource;
            source.populateItems();
        }

        private static void DirectoryPathProperty_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var source = obj as PhoneFileDataSource;
            if (!source.appFiles.DirectoryExists(e.NewValue.ToString()))
                source.DirectoryPath = e.OldValue.ToString();
            source.populateItems();
        }

        public void DeleteFile(string name)
        {
            appFiles.DeleteFile(System.IO.Path.Combine(this.DirectoryPath, name));
            populateItems();
        }
        public void DeleteDirectory(string name)
        {
            appFiles.DeleteDirectory(System.IO.Path.Combine(this.DirectoryPath, name));
            populateItems();
        }
        public void CreateDirectory(string name)
        {
            //appFiles.CreateDirectory(System.IO.Path.Combine(this.DirectoryPath, name));
            appFiles.CreateDirectory(name);
            populateItems();
        }
        public void CreateFile(string name)
        {
            //appFiles.CreateFile(System.IO.Path.Combine(this.DirectoryPath, name));
            appFiles.CreateFile(name);

            populateItems();
        }


        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    public enum FileSystemItemTypes
    {
        Directory,
        File
    }
    public class FileSystemItem
    {
        public FileSystemItemTypes FileSystemType { get; set; }
        public string Name { get; set; }
        public string FullPath { get; set; }
        public FileSystemItem() { }
    }
}